package com.fzu.ess.common.utils;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by ercha on 2015/10/26.
 */
public class MailUtils {
    private static JavaMailSenderImpl senderImpl;
    private static String sendFrom = "admin@ess.fzu.com";

    static{
        senderImpl = new JavaMailSenderImpl();

        Properties properties = new Properties();
        InputStream in = Object.class.getResourceAsStream("/config.properties");
        try{
            properties.load(in);
            senderImpl.setHost(properties.getProperty("mail.host").trim());
            senderImpl.setUsername(properties.getProperty("mail.username").trim());
            senderImpl.setPassword(properties.getProperty("mail.password").trim());
            Properties sendProps = new Properties();
            sendProps.put("mail.smtp.auth", properties.getProperty("mail.smtp.auth").trim());
            sendProps.put("mail.smtp.timeout", properties.getProperty("mail.smtp.timeout").trim());
            senderImpl.setJavaMailProperties(sendProps);

            sendFrom = properties.getProperty("mail.sendFrom").trim();
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void sendSimpleMail(String sendTo, String subject, String text){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(sendFrom);
        simpleMailMessage.setTo(sendTo);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        senderImpl.send(simpleMailMessage);
    }

}
